package com.boot.admin.modules.sys.ai.controller;

import com.boot.admin.base.R;
import com.boot.admin.data.domain.BasePage;
import com.boot.admin.modules.sys.ai.model.query.AiRoleQuery;
import com.boot.admin.modules.sys.ai.model.vo.AiRoleVO;
import com.boot.admin.modules.sys.ai.service.AiRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * AI角色管理
 *
 * @author 李飞洋
 * @date 2025/9/24
 */
@RestController
@RequestMapping("/ai/role")
@Tag(name = "AI系统：角色管理")
@RequiredArgsConstructor
public class AiRoleController {
    private final AiRoleService aiRoleService;

    @GetMapping("/query/page")
    @Operation(summary = "分页查询角色")
//    @PreAuthorize("@permission.hashPermission('ai:role:query:page')")
    public R<BasePage<AiRoleVO>> queryPage(HttpServletRequest request, @ParameterObject AiRoleQuery query) {
        BasePage<AiRoleVO> res = this.aiRoleService.queryPage(query);
        return R.OK(res);
    }

    @GetMapping("/query/list")
    @Operation(summary = "列表查询角色")
    public R<List<AiRoleVO>> queryList(HttpServletRequest request, @ParameterObject AiRoleQuery query) {
        List<AiRoleVO> res = this.aiRoleService.queryList(query);
        return R.OK(res);
    }

    @GetMapping("/query/enabled")
    @Operation(summary = "查询启用的角色")
    public R<List<AiRoleVO>> queryEnabled(HttpServletRequest request) {
        List<AiRoleVO> res = this.aiRoleService.queryEnabledRoles();
        return R.OK(res);
    }

    @GetMapping("/")
    @Operation(summary = "角色详情")
    public R<AiRoleVO> detail(HttpServletRequest request, 
                              @Parameter(name = "id", description = "角色ID", required = true) 
                              @RequestParam String id) {
        return this.aiRoleService.detail(id);
    }

    @PostMapping("/save")
    @Operation(summary = "保存角色")
//    @PreAuthorize("@permission.hashPermission('ai:role:save')")
    public R<AiRoleVO> save(HttpServletRequest request, @Validated @RequestBody AiRoleVO roleVO) {
        return this.aiRoleService.save(roleVO);
    }

    @PutMapping("/update/{id}")
    @Operation(summary = "修改角色")
//    @PreAuthorize("@permission.hashPermission('ai:role:update')")
    public R<AiRoleVO> update(HttpServletRequest request, @PathVariable String id,
                              @Validated @RequestBody AiRoleVO roleVO) {
        return this.aiRoleService.updateById(id, roleVO);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色")
//    @PreAuthorize("@permission.hashPermission('ai:role:del')")
    public R<String> delete(HttpServletRequest request, @Validated @RequestBody List<String> ids) {
        if (null == ids || ids.isEmpty()) {
            return R.NG("删除失败，缺少参数");
        }
        return this.aiRoleService.deleteByIds(ids);
    }

    @PutMapping("/enable/{id}")
    @Operation(summary = "启用角色")
//    @PreAuthorize("@permission.hashPermission('ai:role:enable')")
    public R<String> enable(HttpServletRequest request, @PathVariable String id) {
        return this.aiRoleService.enableRole(id, true);
    }

    @PutMapping("/disable/{id}")
    @Operation(summary = "禁用角色")
//    @PreAuthorize("@permission.hashPermission('ai:role:disable')")
    public R<String> disable(HttpServletRequest request, @PathVariable String id) {
        return this.aiRoleService.enableRole(id, false);
    }
}